﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace MaterialTypeRecognition.Shell.UserControls
{
    public class LogBox : RichTextBox
    {
        private readonly InlineCollection Inlines;

        public LogBox()
        {
            var paragraph = new Paragraph();
            Document = new FlowDocument(paragraph);
            Inlines = paragraph.Inlines;
        }

        public Button ClickButton
        {
            get { return (Button)GetValue(ClickButtonProperty); }
            set { SetValue(ClickButtonProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ClickButton.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ClickButtonProperty =
            DependencyProperty.Register("ClickButton", typeof(Button), typeof(LogBox), new FrameworkPropertyMetadata(default(Button), (d, a) =>
            {
                if (d is LogBox logBox)
                {
                    if (a.OldValue is Button button)
                    {
                        button.Click -= logBox.OnClickButtonClick;
                    }

                    if (a.NewValue is Button newButton)
                    {
                        newButton.Click -= logBox.OnClickButtonClick;
                        newButton.Click += logBox.OnClickButtonClick;
                    }
                }
            }));

        private void OnClickButtonClick(object sender, RoutedEventArgs arg)
        {
            CleanLogs();
        }

        public LogInfo LogInfo
        {
            get { return (LogInfo)GetValue(LogInfoProperty); }
            set { SetValue(LogInfoProperty, value); }
        }

        private int LogCount = 0;

        public int Max
        {
            get { return (int)GetValue(MaxProperty); }
            set { SetValue(MaxProperty, value); }
        }

        private void CleanLogs()
        {
            Inlines.Clear();
            LogCount = 0;
        }

        // Using a DependencyProperty as the backing store for Max.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MaxProperty =
            DependencyProperty.Register("Max", typeof(int), typeof(LogBox), new PropertyMetadata(1));

        // Using a DependencyProperty as the backing store for LogInfo.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LogInfoProperty =
            DependencyProperty.Register("LogInfo", typeof(LogInfo), typeof(LogBox), new FrameworkPropertyMetadata(default(LogInfo), (d, e) =>
            {
                if (d is LogBox logBox)
                {
                    logBox.LogCount++;

                    if (logBox.LogCount > logBox.Max)
                    {
                        logBox.Inlines.Remove(logBox.Inlines.ElementAt(0));
                        logBox.Inlines.Remove(logBox.Inlines.ElementAt(0));
                    }

                    logBox.Inlines.Add(new Run()
                    {
                        Text = ((LogInfo)e.NewValue).Message,
                        Foreground = ((LogInfo)e.NewValue).Brush
                    });
                    logBox.Inlines.Add(new LineBreak());
                    logBox.ScrollToEnd();
                }
            }));
    }
}
